﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using QuanLyTruongHoc_App.MyDataSet;

namespace QuanLyTruongHoc_App
{
    public partial class AddEditMonHoc : Form
    {
        private int currID = 0;
        private Boolean editMode = false;
        private DataRow editRow;

        public AddEditMonHoc()
        {
            InitializeComponent();
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void AddEditGiaoVien_Load(object sender, EventArgs e)
        {
            refreshData();
            refreshID();
        }

        private void refreshID()
        {
            if (!editMode)
                currID = PublicLib.getMaxIdFromTbl(qLTH.tbl_MonHoc.TableName, qLTH.tbl_MonHoc.idColumn.ColumnName) + 1;
        }

        private void refreshData()
        {
            // Change Connection String
            tbl_LoaiMonHocTableAdapter.Connection.ConnectionString =
                PublicLib.getStrConn();

            this.tbl_LoaiMonHocTableAdapter.Fill(this.qLTH.tbl_LoaiMonHoc);
        }

        public void setEditInfo(int _id)
        {
            try
            {
                DataRow[] newRow;
                MyDataSet.QLTHTableAdapters.tbl_MonHocTableAdapter adap = 
                    new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_MonHocTableAdapter();
                adap.Connection.ConnectionString = PublicLib.getStrConn();
                adap.Fill(qLTH.tbl_MonHoc);

                newRow = qLTH.tbl_MonHoc.Select(qLTH.tbl_LopHoc.idColumn.ColumnName + "=" + _id.ToString());
                if (newRow.Length == 0)
                    return;
                editRow = newRow[0];
                currID = _id;

                tb_Ma.Text = editRow[qLTH.tbl_MonHoc.MaMonHocColumn].ToString();
                tb_Ten.Text = editRow[qLTH.tbl_MonHoc.TenMonHocColumn].ToString();
                cb_KieuLop.SelectedValue = editRow[qLTH.tbl_MonHoc.LoaiMonHocColumn];
                tb_SoTiet.Text = float.Parse(editRow[qLTH.tbl_MonHoc.SoTietColumn].ToString()).ToString();
                tb_HeSo.Text = float.Parse(editRow[qLTH.tbl_MonHoc.HeSoColumn].ToString()).ToString(PConst.LAM_TRON);

                chk_DiemHK.Checked = (Boolean)editRow[qLTH.tbl_MonHoc.DungDiemHocKyColumn];
                chk_TinhLenLop.Checked = (Boolean)editRow[qLTH.tbl_MonHoc.TinhLenLopColumn];

                editMode = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi :" + ex.Message);
            }
        }

        private Boolean checkInfo()
        {
            float output;
            int tmp;
            if (!int.TryParse(tb_SoTiet.Text, out tmp))
            {
                MessageBox.Show(MSG.MSG_NHAP_SO);
                tb_SoTiet.Focus();
            }

            if (!float.TryParse(tb_HeSo.Text,out output))
            {
                MessageBox.Show(MSG.MSG_NHAP_SO);
                tb_HeSo.Focus();
            }

            return true;
        }

        private void bt_ThemMoi_Click(object sender, EventArgs e)
        {
            if (!checkInfo())
                return;

            try
            {
                refreshID();
                if (!editMode)
                    editRow = qLTH.tbl_MonHoc.NewRow();

                editRow[qLTH.tbl_MonHoc.idColumn] = currID;
                editRow[qLTH.tbl_MonHoc.MaMonHocColumn] = tb_Ma.Text;
                editRow[qLTH.tbl_MonHoc.TenMonHocColumn] = tb_Ten.Text;
                editRow[qLTH.tbl_MonHoc.LoaiMonHocColumn] = cb_KieuLop.SelectedValue;
                editRow[qLTH.tbl_MonHoc.TinhLenLopColumn] = chk_TinhLenLop.Checked;
                editRow[qLTH.tbl_MonHoc.DungDiemHocKyColumn] = chk_DiemHK.Checked;
                editRow[qLTH.tbl_MonHoc.HeSoColumn] = float.Parse(tb_HeSo.Text);
                editRow[qLTH.tbl_MonHoc.SoTietColumn] = float.Parse(tb_SoTiet.Text);

                editRow[qLTH.tbl_MonHoc.flagColumn] = PConst.FLAG_NONE;

                MyDataSet.QLTHTableAdapters.tbl_MonHocTableAdapter adap = new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_MonHocTableAdapter();
                adap.Connection.ConnectionString = PublicLib.getStrConn();

                if (!editMode)
                    qLTH.tbl_MonHoc.Rows.Add(editRow);

                adap.Update(editRow);
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(MSG.MSG_ERROR_INSERT_DB + " :" + ex.Message);
            }
        }

        private void AddEditMonHoc_Shown(object sender, EventArgs e)
        {
            tb_Ma.Focus();
        }
    }
}
